﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using JasRibbonApplicatie.Classes;
using JasRibbonApplicatie.Converters;

namespace JasRibbonApplicatie.Views
{
    /// <summary>
    /// Interaction logic for MagazijniersPlanning.xaml
    /// </summary>
    public partial class MagazijniersPlanning : Page
    {
        private const string STANDAARD_UUR = "19:00";
        private DateTime dateFrom;
        private PlanningMagazijnier planning;
        
        public MagazijniersPlanning()
        {
            InitializeComponent();
            sheduleFromDatePicker.SelectedDate = DateTime.Now.Date;
            List<Gebruiker> gebruikersList = Queries.GetAllGebruikers();
            searchComboBox.ItemsSource = gebruikersList.Where(a => a.IsMagazijnier == true).OrderBy(a => a.Naam);
            searchComboBox.DisplayMemberPath = "Naam";
            searchComboBox.DisplayMemberPath += "Voornaam";
            sheduleGrid.ItemsSource = Queries.GetPlanningMagazijniers().Where(a => a.Datum >= dateFrom);
            UIHelpers.timePickerComboBoxOpvullen(timePickerComboBox);
            timePickerComboBox.SelectedItem = STANDAARD_UUR;
            magazijniersComboBox.ItemsSource = Queries.GetMagazijniers();
            magazijniersComboBox.DisplayMemberPath = "Gebruiker.NaamVoornaam";
            CalendarRedLetterConverter.dict = Queries.GetMagazijnierNaamAndDate(DateTime.Now.Year);
            
        }

        private void searchComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Gebruiker user = (Gebruiker)searchComboBox.SelectedItem;
            sheduleGrid.ItemsSource = Queries.GetPlanningMagazijniersByGebruikersId(user.Gebruiker_id).Where(a => a.Datum >= dateFrom);
        }

        private void calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
            sheduleGrid.ItemsSource = Queries.GetPlanningMagazijniersByDatum((DateTime)calendar.SelectedDate);
        }

        private void addToPlanning_Click(object sender, RoutedEventArgs e)
        {
            if ((datePicker.SelectedDate != null) && (magazijniersComboBox.SelectedValue != null) && (timePickerComboBox.SelectedValue != null))
            {
                DateTime date = (DateTime)datePicker.SelectedDate;
                TimeSpan time = TimeSpan.Parse(timePickerComboBox.SelectedValue.ToString());

                DateTime dt = date.Add(time);
                Magazijnier magazijnier = (Magazijnier) magazijniersComboBox.SelectedItem;

                if (!Queries.PlanningBestaat(dt))
                {
                    planningmagazijniers pl = new planningmagazijniers();
                    pl.magazijnier_id = magazijnier.MagazijnierId;
                    pl.datum = dt;

                    Queries.entities.AddToplanningmagazijniers(pl);
                    Queries.entities.SaveChanges();

                    sheduleGrid.ItemsSource = Queries.GetPlanningMagazijniersByMagazijnierIdAndDate(pl.magazijnier_id, pl.datum);

                    CalendarRedLetterConverter.dict = Queries.GetMagazijnierNaamAndDate(DateTime.Now.Year);
                }
                else
                {
                    MessageBox.Show("Er is al iemand gepland op dit uur!", "Opgelet!", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                
            }
        }

        private void overzichtButton_Click(object sender, RoutedEventArgs e)
        {
            sheduleGrid.ItemsSource = Queries.GetPlanningMagazijniers().Where(a => a.Datum >= dateFrom);
        }

        private void sheduleFromDatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            DateTime date = (DateTime)sheduleFromDatePicker.SelectedDate;
            dateFrom = date;
        }

        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            if (planning != null)
            {
                var q = from entry in Queries.entities.planningmagazijniers
                        where entry.magazijnier_id == planning.Magazijnier.MagazijnierId && entry.datum == planning.Datum
                        select entry;
                foreach (var ent in q)
                {
                   Queries.entities.DeleteObject(ent);
                }
                Queries.entities.SaveChanges();

                sheduleGrid.ItemsSource = Queries.GetPlanningMagazijniers().Where(a => a.Datum >= dateFrom);
            }
        }

        private void sheduleGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            planning = (PlanningMagazijnier)sheduleGrid.SelectedItem;
        }




    }
}
